local skel = fk.CreateSkill {
  name = "emo__dili",
}

Fk:loadTranslationTable{
  ["emo__dili"] = "地疠",
  [":emo__dili"] = "你失去过【闪】的回合结束时，你可视为使用一张雷【杀】，若未指定当前回合者，其可以发动你的〖人阖〗。",

  ["#emo__dili-lightning"] = "地疠：你可以令 %src 进行【闪电】判定。",
  ["#emo__dili-slash"] = "地疠：你可视为使用一张雷【杀】",
  ["#emo__renhe-other"] = "你可以发动 %dest 的[人阖]，将一张牌当【%arg】使用",
}

skel:addEffect(fk.TurnEnd, {
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(skel.name) then
      return #player.room.logic:getEventsOfScope(GameEvent.MoveCards, 1, function(e)
        for _, move in ipairs(e.data) do
          if move.from == player then
            for _, info in ipairs(move.moveInfo) do
              if (info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip)
                and Fk:getCardById(info.cardId).trueName == "jink" then
                return true
              end
            end
          end
        end
        return false
      end, Player.HistoryTurn) > 0
    end
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local use = room:askToUseVirtualCard(player, {
      name = "thunder__slash", cancelable = true, skill_name = skel.name, skip = true, extra_data = {bypass_times = true},
      prompt = "#emo__dili-slash",
    })
    if use then
      event:setCostData(self, use)
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local use = event:getCostData(self)
    room:useCard(use)
    if player:hasSkill("emo__renhe") and use.tos and not table.contains(use.tos, target) and not target.dead then
      local name = "iron_chain"
      if player:getSwitchSkillState("emo__renhe") == fk.SwitchYin then
        name = target:getMark("emo__renhe_record-turn")
        if name == 0 then return end
      end
      if not target:canUse(Fk:cloneCard(name), {bypass_times = true}) then return end
      local use2 = room:askToUseVirtualCard(target, {
        name = name, cancelable = true, skill_name = "emo__renhe", skip = true, extra_data = {bypass_times = true},
        card_filter = { n = {1, 1}, pattern = "." },
        prompt = "#emo__renhe-other::" .. player.id .. ":" .. name
      })
      if not use2 then return end
      -- 先切换转化技
      room:setPlayerMark(
        player,
        MarkEnum.SwithSkillPreName .. "emo__renhe",
        player:getSwitchSkillState("emo__renhe", true)
      )
      player:addSkillUseHistory("emo__renhe") -- updata UI
      room:useCard(use2)
    end
  end,
})

--[[
-- 废案 其他角色的出牌阶段结束时，若其本阶段对你造成过/未造成伤害，你可令其进行【闪电】判定/其可以发动你的〖人阖〗。
skel:addEffect(fk.EventPhaseEnd, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    return target.phase == Player.Play and player:hasSkill(skel.name) and not target.dead and target ~= player
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local cost_data = {}
    if #room.logic:getActualDamageEvents(1, function(e)
      return e.data.from == target and e.data.to == player
    end, Player.HistoryPhase) > 0 then
      if room:askToSkillInvoke(player, {skill_name = skel.name, prompt = "#emo__dili-lightning:"..target.id }) then
        event:setCostData(self, cost_data)
        return true
      end
    elseif player:hasSkill("emo__renhe") then
      local name = "iron_chain"
      if player:getSwitchSkillState("emo__renhe") == fk.SwitchYin then
        name = target:getMark("emo__renhe_record-turn")
        if name == 0 then return end
      end
      -- 有次数限制
      if not target:canUse(Fk:cloneCard(name)) then return end
      local use = room:askToUseVirtualCard(target, {
        name = name, cancelable = true, skill_name = skel.name, skip = true, extra_data = {bypass_times = false},
        card_filter = { n = {1, 1}, pattern = "." },
        prompt = "#emo__renhe-other::" .. player.id .. ":" .. name
      })
      if use then
        cost_data.use = use
        event:setCostData(self, cost_data)
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cost_data = event:getCostData(self)
    if cost_data.use == nil then
      local judge = {
        who = target,
        reason = "lightning",
        pattern = ".|2~9|spade",
      }
      room:judge(judge)
      if judge:matchPattern() then
        room:damage{
          to = target,
          damage = 3,
          damageType = fk.ThunderDamage,
          skillName = skel.name,
        }
      end
    else
      -- 先切换转化技
      room:setPlayerMark(
        player,
        MarkEnum.SwithSkillPreName .. "emo__renhe",
        player:getSwitchSkillState("emo__renhe", true)
      )
      player:addSkillUseHistory("emo__renhe") -- updata UI
      room:useCard(cost_data.use)
    end
  end,
})
--]]

return skel
